package musicstore.web.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import musicstore.biz.ShoppingCartBiz;
import musicstore.entity.ShoppingCart;

@Controller
public class ShoppingCartController {
	@Autowired
	private ShoppingCartBiz cartBiz;
	@RequestMapping("/cart-add")
	public String cartAdd(int id, HttpSession session){
		ShoppingCart cart =(ShoppingCart)session.getAttribute("cart");
		if(cart==null){
			cart = new ShoppingCart();
			session.setAttribute("cart", cart);
		}
		cartBiz.addItemToCart(cart, id);
		return "redirect:/cart";
	}
	@RequestMapping("/cart")
	public String cart(){
		return "cart";
	}
	@RequestMapping("/cart-delete")
	public String cartDelete(int id, HttpSession session){
		ShoppingCart cart =(ShoppingCart)session.getAttribute("cart");
		if(cart==null){
			cart = new ShoppingCart();
			session.setAttribute("cart", cart);
		}
		cartBiz.removeItemToCart(cart, id);
		return "redirect:/cart";
	}
}
